package com.crm.miaohe.service;

import com.crm.miaohe.controller.report.response.DashboardResponse;
import com.crm.miaohe.mapper.TblCustomerMapper;
import com.crm.miaohe.mapper.TblOrderMapper;

import com.crm.miaohe.mapper.TblPriceStrategyMapper;
import com.crm.miaohe.model.TblCustomerExample;
import com.crm.miaohe.model.TblOrder;
import com.crm.miaohe.model.TblOrderExample;
import com.crm.miaohe.model.TblPriceStrategyExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class DashboardServiceImpl implements DashboardService {

    private final TblOrderMapper tblOrderMapper;
    private final TblCustomerMapper tblCustomerMapper;
    private final TblPriceStrategyMapper tblPriceStrategyMapper;

    public DashboardServiceImpl(
            TblCustomerMapper tblCustomerMapper,
            TblPriceStrategyMapper tblPriceStrategyMapper,
            TblOrderMapper tblOrderMapper) {
        this.tblOrderMapper = tblOrderMapper;
        this.tblCustomerMapper = tblCustomerMapper;
        this.tblPriceStrategyMapper = tblPriceStrategyMapper;
    }

    @Override
    public DashboardResponse getDashboardData() {

        //审批过的会员
        TblCustomerExample tblCustomerExample = new TblCustomerExample();
        tblCustomerExample.createCriteria().andApprovedEqualTo(true);
        Long customerCount = tblCustomerMapper.countByExample(tblCustomerExample);

        //价格策略
        TblPriceStrategyExample tblPriceStrategyExample = new TblPriceStrategyExample();
        tblPriceStrategyExample.createCriteria().andApprovedEqualTo(true);
        Long priceStrategyCount = tblPriceStrategyMapper.countByExample(tblPriceStrategyExample);

        //订单数，订单总金额
        TblOrderExample tblOrderExample = new TblOrderExample();
        tblOrderExample.createCriteria().andStatusEqualTo(1);
        List<TblOrder> list = tblOrderMapper.selectByExample(tblOrderExample);
        //总金额
        Double amount = list.stream().mapToDouble(TblOrder::getAmount).sum();
        //订单数
        Integer num = list.size();

        return DashboardResponse.builder()
                .customerCount(customerCount)
                .priceStrategyCount(priceStrategyCount)
                .orderCount(num)
                .orderAmount(amount)
                .build();
    }

}
